/* @(#)GSOrderDetailQueryConfirmAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-2-23     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSBalanceParamBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSBalanceProcessor;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSOrgProcessor;
import com.hitrust.gs.dao.GSBalanceParamDAO;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSBalanceAuditConfirmForm;
import com.hitrust.gs.form.GSOrderDetailQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;

/**
 * 巴士物资人员执行订单明细查询
 */
public class GSBalanceAuditConfirmAction extends GSBaseAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
	 */
	public String execute(GSBaseForm aForm) throws Exception {
        GSBalanceAuditConfirmForm tForm = (GSBalanceAuditConfirmForm)aForm;

        try{
            //取得当前操作员可查询的订货单位和送货地址
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            GSOrderDeptInfoProcessor tProc = new GSOrderDeptInfoProcessor();
            HashMap tMap = tProc.getOrderDeptInfoOfBusOper(tOperBean.getGs_oper_org_id());
            aForm.getRequest().setAttribute("orderdept",tMap);
            //取得系统设定的结算日期
            GSBalanceParamDAO tBalParmDAO = new GSBalanceParamDAO();
            GSBalanceParamBean tBean = tBalParmDAO.queryBalanceDate();
            String tCurDay = DateUtil.getCurrentDate("yyyyMMdd");
            if(tBean.getGs_balance_date_type().equals(GSBaseConstants.NATURE_BALANCE_DATE)){//自然月参数类型
                int maxday = DateUtil.getMaxMonthDay(tCurDay);
                tBean.setGs_balance_start_date("1");
                tBean.setGs_balance_end_date(String.valueOf(maxday));
                aForm.getRequest().setAttribute("balancestrdate",DateUtil.formateDateTimeForUser(DateUtil.getSpecDay(1)));
                aForm.getRequest().setAttribute("balanceenddate",DateUtil.formateDateTimeForUser(DateUtil.getSpecDay(maxday)));
            }else{//手动设定参数类型
                int tStrDay = Integer.parseInt(tBean.getGs_balance_start_date());
                int tEndDay = Integer.parseInt(tBean.getGs_balance_end_date());
                int curYear = Integer.parseInt(tCurDay.substring(0,4));
                int curMonth = Integer.parseInt(tCurDay.substring(4,6));
                if(tStrDay > tEndDay || tStrDay == tEndDay){    //开始日期大于等于结束日期
                    if(tCurDay.substring(4,6).equals("01")){    //如果当前月份为1月,则开始区间的年为上一年份
                        aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear-1,12,tStrDay,"yyyy/MM/dd"));    
                        aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,1,tEndDay,"yyyy/MM/dd"));
                    }else {
                        aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear,curMonth - 1,tStrDay,"yyyy/MM/dd"));    
                        aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,curMonth,tEndDay,"yyyy/MM/dd"));
                    }  
                }else if(tStrDay < tEndDay){        //开始日期小于结束日期
                    aForm.getRequest().setAttribute("balancestrdate",DateUtil.getSpecDate(curYear,curMonth,tStrDay,"yyyy/MM/dd"));    
                    aForm.getRequest().setAttribute("balanceenddate",DateUtil.getSpecDate(curYear,curMonth,tEndDay,"yyyy/MM/dd"));
                }
            }
            aForm.getRequest().setAttribute("balanceparm",tBean);
            
            GSBalanceProcessor tBalProc = new GSBalanceProcessor();
            //执行结算导入
            boolean tFlag = tBalProc.balanceInfoRecordSelected(tForm);
            if(tFlag){
                aForm.getRequest().setAttribute("message","对帐数据生成成功");
            }else{
                aForm.getRequest().setAttribute("message","对帐数据生成失败");
            }
        }catch(GSException ge){
        	aForm.getRequest().setAttribute("message",ge.getMessage());
        }catch(Exception e){
            aForm.getRequest().setAttribute("action","A180801.do");
            throw new GSException(e.toString());
        }
        
        //执行查询
		try {
			tForm.getRequest().setAttribute("orderid", tForm.getOrderid());
			tForm.getRequest().setAttribute("providerName", tForm.getProviderName());
			tForm.getRequest().setAttribute("concentricityId", tForm.getConcentricityId());
			tForm.getRequest().setAttribute("orderCreateDateStr", tForm.getOrderCreateDateStr());
			tForm.getRequest().setAttribute("orderCreateDateEnd", tForm.getOrderCreateDateEnd());
			tForm.getRequest().setAttribute("manufInfo", tForm.getManufInfo());
			tForm.getRequest().setAttribute("orderDetailStatus", tForm.getOrderDetailStatus());
			tForm.getRequest().setAttribute("deliverGoodsId", tForm.getDeliverGoodsId());
			tForm.getRequest().setAttribute("orderType", tForm.getOrderType());
            
            String[] tDateStr = new String[2];
            tDateStr[0] = tForm.getOrderCreateDateStr();
            tDateStr[1] = tForm.getOrderCreateDateEnd();
            tDateStr = DateUtil.formateDate(tDateStr);
            tForm.setOrderCreateDateStr(tDateStr[0]);
            tForm.setOrderCreateDateEnd(tDateStr[1]);
            
			GSOperatorBean tOperatorBean = (GSOperatorBean) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
			// 取得当前操作员可操作企业及下级企业的订货地址
			GSOrderDeptInfoProcessor tOrderDeptInfoProc = new GSOrderDeptInfoProcessor();
			HashMap tMap = tOrderDeptInfoProc.getOrderDeptInfoOfBusOper(tOperatorBean.getGs_oper_org_id());
			aForm.getRequest().setAttribute("orderdept", tMap);
			
			GSOrderProcessor tProc = new GSOrderProcessor();
			ArrayList tOrg = (ArrayList)tMap.get("organization");
            
            
	        GSOrderDetailQueryForm sForm = new GSOrderDetailQueryForm();
	    	sForm.setOrderid(tForm.getOrderid());
	    	sForm.setProviderName(tForm.getProviderName());
	    	sForm.setOrderDeptId(tForm.getOrderDeptId());
	    	sForm.setDeliverGoodsAddrId(tForm.getDeliverGoodsAddrId());
	    	sForm.setConcentricityId(tForm.getConcentricityId());
	    	sForm.setManufInfo(tForm.getManufInfo());
	    	sForm.setManuftype1(tForm.getManuftype1());
	    	sForm.setManuftype2(tForm.getManuftype2());
	    	sForm.setManuftype3(tForm.getManuftype3());
	    	sForm.setOrderCreateDateStr(tForm.getOrderCreateDateStr());
	    	sForm.setOrderCreateDateStr(tForm.getOrderCreateDateStr());
	    	sForm.setOrderDetailStatus(tForm.getOrderDetailStatus());
	    	sForm.setDeliverGoodsId(tForm.getDeliverGoodsId());
	    	sForm.setOrderType(tForm.getOrderType());
			//取得分页参数
			String[] tStr = this.getPageParams(sForm,tOrg);
            //执行分页查询
			ArrayList tList = tProc.getOrderDetailListForBalanceAudit(sForm,tOrg,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
			ResultProcess tRsProc = new ResultProcess();
			tRsProc.setResult(tList);
			tForm.getRequest().setAttribute("order", tRsProc);
            
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
			
			//取得订单明细状态
			aForm.getRequest().setAttribute("orderdetailstatuslist", getOrderDetailStatusList());
			
			//取得订单明细状态
			aForm.getRequest().setAttribute("ordertypelist", getOrderTypeList());
            
            //取得产品分类
            GSManufactureProcessor tManufProc = new GSManufactureProcessor();
            tManufProc.getManufactureOfLevel(aForm);

		} catch (Exception e) {
			tForm.getRequest().setAttribute("action", "A180801.do");
			throw e;
		}

        
        return GSBaseConstants.FORWARD_SUCCESS;
        

	}
	
	/**
     * 取得订单明细状态
     * @return 订单明细状态列表
     */
    private ArrayList getOrderDetailStatusList() {
    	ArrayList tResult = new ArrayList();
    	
    	String [] tArray = new String[2];
    	tArray = new String[2];
    	tArray[0] = "12";
    	tArray[1] = "交货确认";
    	tResult.add(tArray);
    	
    	return tResult;
    }
    
    private ArrayList getOrderTypeList() {
    	ArrayList tResult = new ArrayList();
    	String [] tArray = new String[2];
    	tArray[0] = "0";
    	tArray[1] = "正常采购";
    	tResult.add(tArray);
    	
    	tArray = new String[2];
    	tArray[0] = "1";
    	tArray[1] = "应急采购";
    	tResult.add(tArray);
    	    	
    	return tResult;
    }
    private String[] getPageParams(GSOrderDetailQueryForm aForm,ArrayList aOrg) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSOrderDAO tDAO = new GSOrderDAO();
            //totalRecords = tDAO.getOrderDetailListCount(aForm,aOrg);
            totalRecords = tDAO.getOrderDetailListCount1(aForm,aOrg);
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }

}
